package com.tianqu.tms.www.bid.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.AucCarInfo;
import com.tianqu.tms.entity.AucManagement;
import com.tianqu.tms.www.bid.viewhelper.BidCarQuotationViewHelper;

public class BidCarQuotationWaitListController extends TmsController {
	
	BidCarQuotationViewHelper view = new BidCarQuotationViewHelper();
	private Logger logger = Logger.getLogger(BidCarQuotationWaitListController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		
		logger.debug(this.getClass().getName() + "#execute was called.");
		// 查询等待竞价列表信息
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			AucManagement aucManage = AucManagement.getInstance();
			AucCarInfo aucCarinfo = AucCarInfo.getInstance();
			List<AucManagement> list = aucManage.getAucManagementList();
			
			if (list != null && list.size() > 0) {
				List<BidCarQuotationViewHelper> viewList = new ArrayList<BidCarQuotationViewHelper>();
				int j = 1;
				for (int i = 0; i < list.size(); i++) {
					AucManagement auc = list.get(i);
					
					// 等待寻价
					if (auc.getBidStatus() == 1) {
						BidCarQuotationViewHelper view = new BidCarQuotationViewHelper();
						aucCarinfo = aucCarinfo.getaucCarBase(auc.getSerial());
						
						//序号
						view.setNum(j);
						
						//竞价表主键ID
						view.setId(String.valueOf(auc.getId()));
						
						//车辆ID
						view.setSerial(aucCarinfo.getSerial());
						
						//车辆名称
						view.setCarName(aucCarinfo.getCarName());
						
						//车牌号码
						view.setLicensePlate(aucCarinfo.getLicensePlate());
						
						//车架号
						view.setVin(aucCarinfo.getVin());
						
						// 竞价状态
						view.setBidStatus(auc.getBidStatus());
						
						//登记日期
						view.setRegDate(aucCarinfo.getRegDate());
						
						//表显里程(公里)
						view.setMileage(StringUtil.getPointBetweenThree(aucCarinfo.getMileage()));
						
						//开始时间
						view.setStartTime(DateUtil.DateToString13(auc.getStartTime()));
						
						//竞价时长(分钟)
						view.setAuctionDeadline(String.valueOf(auc.getAuctionDeadline()));

						//样式
						view.setCssFlag(auc.getCssFlag());
						
						viewList.add(view);
						this.view.setBidCarQuotationWaitNum(this.view.getBidCarQuotationWaitNum() + 1);
						j++;
					} else if (auc.getBidStatus() == 2) {
						//寻价开始
						view.setBidCarQuotationStartNum(view.getBidCarQuotationStartNum() + 1);
					} else if (auc.getBidStatus() == 3 || auc.getBidStatus() == 4 || auc.getBidStatus() == 5 || auc.getBidStatus() == 6) {
						//寻价结束（竞价结束，成交，流拍，回访成交 modify by mufeiyu）
						view.setBidCarQuotationFinishNum(view.getBidCarQuotationFinishNum() + 1);
					}
				}

				view.setBidCarQuotationWaitList(viewList);
			}
			
			//显示内容
//			AuctionRun run = AuctionRun.getInstance();
//			view.setBidStartTime(run.getNowAucStartTime());

			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

}
